Skip to content

Adds pytest_markdown_docs_markdown_it for custom markdownit parser - #47

Merged
freider merged 3 commits into
modal-labs:mainfrom
thomasjpfan:add_markdownit_plugin
Apr 9, 2025
Merged

Adds pytest_markdown_docs_markdown_it for custom markdownit parser#47
freider merged 3 commits into
modal-labs:mainfrom
thomasjpfan:add_markdownit_plugin

Conversation

@thomasjpfan

Copy link
Copy Markdown
Contributor

Issue: Fixes #31

This PR adds a pytest_markdown_docs_markdown_it pytest hook to allow developers to customize the markdown_it parser. This means they can add plugins such as admon_plugin to the parser.

An alternative is to add something like https://github.com/modal-labs/pytest-markdown-docs/blob/main/src/pytest_markdown_docs/_runners.py, but using a pytest hook feels a little more natural with how pytest does things. For example, pytest-xdist has uses a pytest_xdist_make_scheduler to configure a custom scheduler:

https://github.com/pytest-dev/pytest-xdist/blob/f83299cd646965ac0560adddcf7204adde9f486e/src/xdist/newhooks.py#L96-L100

@freider

freider commented Apr 8, 2025

Copy link
Copy Markdown
Collaborator

Thanks for the contribution! I like the pytest hook pattern for this, since the markdown flavor is probably going to be the same across a codebase anyways.

Comment thread src/pytest_markdown_docs/plugin.py Outdated


def extract_fence_tests(
markdown_it_parsers: typing.List["MarkdownIt"],

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be missing something here, but why is this a list? (seems like we are throwing away anything except the first element anyway)

@thomasjpfan thomasjpfan Apr 8, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pytest returns a list because the hook can be defined multiple times. For example:

tests/
    conftest.py <- hook can be defined here
    io/
        conftest.py <- same hook can be defined here
        my_test.py

The default is to have a list that contains all the hooks and the nested conftest.py gets called first so they are first in the hooks list.


I simplified the code by using @pytest.hookspec(firstresult=True), which returns the first one by default: 0be6a10

Also, 5fe7f6f, which defines a default MarkdownIt parser using the hook.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, yeah this makes sense! Thanks for the explanation and I like the firstresult and default implementation

Comment thread src/pytest_markdown_docs/plugin.py Outdated
or "<Unnamed obj>"
)
fence_syntax = FenceSyntax(self.config.option.markdowndocs_syntax)
markdown_it_parsers = (

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the list comment above - maybe it's better to validate the hook return value here (making sure it's at most one specification, since that's what we support) and then just pass in the single MarkdownIt we want to use?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With 0be6a10 and 5fe7f6f, this should be a single MarkdownIt object.

The remaining user error is returning a non-MarkdownIt object:

def pytest_markdown_docs_markdown_it():
    return "abc"

Do you think it's worth checking for this edge case?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the other changes I don't think we need to check for anything here 👍

@freider freider left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, thanks!

@freider
freider merged commit c932861 into modal-labs:main Apr 9, 2025
@freider freider mentioned this pull request Apr 9, 2025
@staadecker staadecker mentioned this pull request May 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Not compatible with mkdocs's admonition

2 participants